<?php

namespace app\admin\model\zhongtai;

use think\Db;
use think\Model;

class ShoproOrder extends Model
{
    // 表名
    protected $table = 'shopro_order';
    // 设置当前模型的数据库连接
    protected $connection = 'db_zhongtai';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    /**
     * 获取最新的更新时间
     */
    public function getUpTime($manage_id)
    {
        $updatetime = $this->where('manage_id', $manage_id)->order("zt_time desc")->value("zt_time");
        return $updatetime ?? 0;
    }

    public function upData($data, $manage_id)
    {
        if ($data) {
            $con = count($data);
            foreach ($data as $k => $v) {
                echo $k . PHP_EOL;
                if (($con - 1) == $k) {
                    $zttime = date('Y-m-d H:i:s',strtotime($v['zt_time'])+1);
                }else{
                    $zttime = $v['zt_time'];
                }
                $user_data = (new User())->getUserStatus($manage_id,$v['user_id']);
                if(!$user_data) continue;
                $in_data = [
                    "manage_id" => $manage_id,
                    "order_id" => $v['id'],
                    "user_id" => $v['user_id'],
                    "type" => $v['type'],
                    "is_require_link_goods" => $v['is_require_link_goods'],
                    "link_url" => $v['link_url'],
                    "order_sn" => $v['order_sn'],
                    "activity_type" => $v['activity_type'],
                    "goods_amount" => $v['goods_amount'],
                    "dispatch_amount" => $v['dispatch_amount'],
                    "phone" => $v['phone'],
                    "consignee" => $v['consignee'],
                    "province_name" => $v['province_name'],
                    "city_name" => $v['city_name'],
                    "area_name" => $v['area_name'],
                    "address" => $v['address'],
                    "province_id" => $v['province_id'],
                    "city_id" => $v['city_id'],
                    "area_id" => $v['area_id'],
                    "status" => $v['status'],
                    "invoice_status" => $v['invoice_status'],
                    "memo" => $v['memo'],
                    "remark" => $v['remark'],
                    "total_amount" => $v['total_amount'],
                    "score_amount" => $v['score_amount'],
                    "total_fee" => $v['total_fee'],
                    "discount_fee" => $v['discount_fee'],
                    "coupon_fee" => $v['coupon_fee'],
                    "activity_discount_money" => $v['activity_discount_money'],
                    "dispatch_discount_money" => $v['dispatch_discount_money'],
                    "pay_fee" => $v['pay_fee'],
                    "score_fee" => $v['score_fee'],
                    "goods_original_amount" => $v['goods_original_amount'],
                    "coupons_id" => $v['coupons_id'],
                    "transaction_id" => $v['transaction_id'],
                    "payment_json" => $v['payment_json'],
                    "pay_type" => $v['pay_type'],
                    "paytime" => $v['paytime'],
                    "ext" => $v['ext'],
                    "platform" => $v['platform'],
                    "xw_orderNo" => $v['xw_orderNo'],
                    "xw_account" => $v['xw_account'],
                    "xw_code" => $v['xw_code'],
                    "xw_pay_url" => $v['xw_pay_url'],
                    "xw_is_default" => $v['xw_is_default'],
                    "xw_create_time" => $v['xw_create_time'],
                    "xw_query_num" => $v['xw_query_num'],
                    "ip" => $v['ip'],
                    "province" => $v['province'],
                    "city" => $v['city'],
                    "is_share" => $v['is_share'],
                    "goods_id" => $v['goods_id'],
                    "one_day" => $v['one_day'],
                    "share_retreat" => $v['share_retreat'],
                    "mutual_amount" => $v['mutual_amount'],
                    "cancel_count" => $v['cancel_count'],
                    "is_split" => $v['is_split'],
                    "is_df" => $v['is_df'],
                    "df_remark" => $v['df_remark'],
                    "df_user_id" => $v['df_user_id'],
                    "df_reject" => $v['df_reject'],
                    "df_reject_reason" => $v['df_reject_reason'],
                    "df_reject_remark" => $v['df_reject_remark'],
                    "df_suggest" => $v['df_suggest'],
                    "is_tb" => $v['is_tb'],
                    "sku_id" => $v['sku_id'],
                    "is_old" => $v['is_old'],
                    "is_bill" => $v['is_bill'],
                    "createtime" => $v['createtime'],
                    "zt_time" => $zttime,
                ];
                $user_data = $this->where([
                    'order_id' =>  $v['id'],
                    'manage_id' => $manage_id,
                ])->find();
                if ($user_data) {
                    $this->where([
                        'order_id' =>  $v['id'],
                        'manage_id' => $manage_id,
                    ])->update($in_data);
                } else {
                    $this->insert($in_data);
                }
            }
        }
        echo "shopro_order表更新完成";
    }
    public function getOrderStatus($manage_id,$order_id){
        $data = $this->where(['manage_id'=>$manage_id,'order_id'=>$order_id])->find();
        if($data){
            return true;
        }else{
            return false;
        }
    }
    public function getOrderSnStatus($manage_id,$order_sn){
    $data = $this->where(['manage_id'=>$manage_id,'order_sn'=>$order_sn])->find();
    if($data){
        return true;
    }else{
        return false;
    }
}
}
